package com.populatton.common;

import com.alibaba.fastjson.JSONObject;
import com.populatton.base.RespEnum;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
 * 用于回滚事务所抛出的异常
 * 对运行时异常进行处理
 * @author willie-zhang
 *
 */
@Slf4j
public class RevertException extends RuntimeException{

    private static final long serialVersionUID = -193316907147406110L;

    @Getter
    private Integer code;

    @Getter
    private String message;



	/** Constructs a new runtime exception with {@code null} as its
     * detail message.  The cause is not initialized, and may subsequently be
     * initialized by a call to {@link #initCause}.
     */
    public RevertException() {
        super();
    }

    /** Constructs a new runtime exception with the specified detail message.
     * The cause is not initialized, and may subsequently be initialized by a
     * call to {@link #initCause}.
     *
     * @param   message   the detail message. The detail message is saved for
     *          later retrieval by the {@link #getMessage()} method.
     */
    public RevertException(String message) {
        super(message);
    }

    /**
     * Constructs a new runtime exception with the specified detail message and
     * cause.  <p>Note that the detail message associated with
     * {@code cause} is <i>not</i> automatically incorporated in
     * this runtime exception's detail message.
     *
     * @param  message the detail message (which is saved for later retrieval
     *         by the {@link #getMessage()} method).
     * @param  cause the cause (which is saved for later retrieval by the
     *         {@link #getCause()} method).  (A <tt>null</tt> value is
     *         permitted, and indicates that the cause is nonexistent or
     *         unknown.)
     * @since  1.4
     */




    /**
     * @param respEnum 枚举信息
     */
    public RevertException(RespEnum respEnum, String ... desc){
        super(respEnum.getDesc());
        this.code = respEnum.getCode();
        if (desc != null && desc.length > 0){
            this.message = String.format(respEnum.getDesc(), desc);
        }else{
            this.message = respEnum.getDesc();
        }

        if(log.isInfoEnabled()) {
            log.info(respEnum.getCode() + ":" + respEnum.getDesc());
        }
    }


    /**
     *
     * @param respEnum 枚举信息
     * @param e 异常
     */
    public RevertException(RespEnum respEnum, Throwable e){
        super(respEnum.getDesc(),e);
        if(log.isErrorEnabled()) {
            log.error(respEnum.getCode() + ":" + respEnum.getDesc() + "|cause:" + e.getCause() + "|message:" + e.getMessage(), e);
        }
        this.code = respEnum.getCode();
        this.message = respEnum.getDesc();
    }
    /**
     *
     * @param respEnum 枚举信息
     * @param arguments 参数
     */
    public RevertException(RespEnum respEnum, Object arguments){
        super(respEnum.getDesc());
        if(log.isInfoEnabled()) {
            log.info(respEnum.getCode() + ":" + respEnum.getDesc() + "|arguments:" + JSONObject.toJSONString(arguments));
        }
        this.code = respEnum.getCode();
        if (arguments instanceof String){
            this.message = String.format(respEnum.getDesc(), arguments);
        }else{
            this.message = respEnum.getDesc();
        }

    }

    /**
     *
     * @param respEnum 枚举信息
     * @param e 异常
     * @param arguments 参数
     */
    public RevertException(RespEnum respEnum, Object  arguments, Throwable e){
        super(respEnum.getDesc(),e);
        if(log.isErrorEnabled()) {
            log.error(respEnum.getCode() + ":" + respEnum.getDesc() + "|cause:" + e.getCause() + "|message:" + e.getMessage() + "|arguments:" + JSONObject.toJSONString(arguments), e);
        }
        this.code = respEnum.getCode();
        this.message = respEnum.getDesc();
    }

    /**
     *
     */
    public RevertException(Integer code, String description, Throwable e){
        super(description,e);
        if(log.isErrorEnabled()) {
            log.error(code + ":" + description + "|cause:" + e.getCause() + "|message:" + e.getMessage() + "|arguments:" + e);
        }
        this.code = RespEnum.SERVER_ERROR.getCode();
        this.message =  RespEnum.SERVER_ERROR.getDesc();
    }

    /**
     *
     */
    public RevertException(String description, Throwable e){
        super(description,e);
        if(log.isErrorEnabled()) {
            log.error(description + "|cause:" + e.getCause() + "|message:" + e.getMessage() + "|arguments:" + e);
        }
        this.code = RespEnum.SERVER_ERROR.getCode();
        this.message =  RespEnum.SERVER_ERROR.getDesc();
    }


}
